﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ThreadModule.Data
{
    /// <summary>
    /// 角度弧度转换类
    /// </summary>
    public static class AngleConverter
    {
        /// <summary>
        /// 弧度转角度
        /// </summary>
        /// <param name="rad">弧度值</param>
        /// <returns>角度值</returns>
        public static double ConvertRadToDegree(double rad)
        {
            return 180 / Math.PI * rad;
        }

        /// <summary>
        /// 将小数值转换成度分秒格式
        /// </summary>
        /// <param name="valInDecimal">小数值</param>
        /// <returns>度分秒格式字符串</returns>
        public static string ConvertDecimalToDMS(double valInDecimal)
        {
            // 分离度、分、秒
            int degree = (int)valInDecimal;
            double minutes = (valInDecimal - degree) * 60;
            int minute = (int)minutes;
            double seconds = (minutes - minute) * 60;

            // 输出格式化的度分秒字符串
            string dms = $"{degree}°{minute}′{seconds:0.00}″";
            return dms;
        }

        /// <summary>
        /// 角度转换为弧度
        /// </summary>
        /// <param name="angle">角度值</param>
        /// <returns>弧度值</returns>
        public static double ConvertDegreeToRad(double angle)
        {
            return Math.PI / 180 * angle;
        }

        /// <summary>
        /// 计算两条直线之间的夹角
        /// </summary>
        /// <param name="k1">斜率1</param>
        /// <param name="k2">斜率2</param>
        /// <returns>夹角角度值</returns>
        public static double GetAngleBetweenLines(double k1, double k2)
        {
            if (k1 * k2 == -1)
            {
                return 90.0;
            }
            else
            {
                double angle = ConvertRadToDegree(Math.Atan(Math.Abs((k1 - k2) / (1 + k1 * k2))));
                return angle;
            }
        }
    }
}
